import axios from 'axios'
import { message } from 'ant-design-vue'  
import router  from  '@/router/active'
import store  from  '@/store'

// console.log(message,Spin)
axios.defaults.withCredentials = true
// 创建axios实例
const service = axios.create({
    // api的base_url
    baseURL: process.env.BASE_API,
    // 请求超时时间
    timeout: 10000, 
})

let that = this

// request请求拦截器
service.interceptors.request.use(config => {
    /*请求统一加上loading*/
    // store.commit('showLoading')

    config.url = encodeURI(config.url);
    config.url=process.env.BASE_API + config.url;  
    //这里请求加随机数，是为了解决IE的严重缓存 
    if (config.url.indexOf('?')>0){
        config.url=config.url+'&vmf='+Math.random();
    }else{
        config.url=config.url+'?vmf='+Math.random();
    }
    // Do something before request is sent
    if (sessionStorage.getItem('token')) {
        // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
        config.headers['X-Token'] = sessionStorage.getItem('token')
    }
     // config.headers.post['Content-Type']="application/x-www-form-urlencoded"
     config.headers.post['Content-Type']="application/json"
    return config
}, error => {
    /*请求统一加上loading*/
    // store.commit('hideLoading')

    // Do something with request error
    //console.log(error) // for debug
    // if(error && error.response && error.response.data){
    //   Promise.reject(error.response.data)
    // }
    return Promise.reject(error)
})

// respone响应拦截器
service.interceptors.response.use(
    response => {
        // console.log(response)
        /*请求统一加上loading*/
        // store.commit('hideLoading')
        if(response.data == '身份信息错误,请重新登陆'){
               message.error(response.data)
               /*重新登录函数*/  
               reLogin()
               return false
        }
        if(response.data.flag == false){
            message.error(response.data.msg) 
            return false
        }
        else if(response.status == 400){
            message.error(response.data)
            return false
        }
        else if(response.status == 401){ 
            message.error(response.data)
            return false
        }
        else if(response.status == 403){ 
            message.error(response.data)
            return false
        }
        else if(response.status == 404){ 
            message.error(response.data)
            return false
        }
        else if(response.status == 500){
            message.error('服务器出错，请稍候再试或联系管理员')
            return false
        }
        return response;
    },
    error => {
        console.log(error)
        /*请求统一加上loading*/
        // store.commit('hideLoading')
        if(error.response){
             // promise.all([...]).then().catch(error=>{if(error.response)...})
            switch(error.response.status){
                case 400: 
                    message.error(error.response.data)
                    break;
                case 401:
                    message.error(error.response.data)
                     break;
                case 403:
                    message.error(error.response.data)
                     break;
                case 404: 
                    message.error(error.response.statusText)
                     break;
                case 500:
                    message.error('服务器出错，请联系管理员')
                     break;
                default:
                    message.error(error.response.data)
            }
        }
        else{
            /*request  too  long*/
            // Message({type:'error',message:'请求超时，请稍后再试！'})    
        }
      return Promise.reject(error)
    })

function reLogin(){
    /*清除存储*/
      sessionStorage.clear() 
      store.commit("setusername",'');
      store.commit("setmenudata",[]);
      router.push('/login')
}

export default service
